package cn.msqweb.config;

import cn.hutool.core.util.ObjectUtil;
import cn.msqweb.constant.AuthConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Component
public class FeingInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        if (ObjectUtil.isNotNull(requestAttributes)){
            HttpServletRequest request = requestAttributes.getRequest();
            if (ObjectUtil.isNotNull(request)){

                String  authorization = request.getHeader(AuthConstants.AUTHORIZATION);
                requestTemplate.header(AuthConstants.AUTHORIZATION, authorization);
            }
        }
        requestTemplate.header(AuthConstants.AUTHORIZATION, AuthConstants.BEARER + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VycyI6IntcImFjY291bnROb25FeHBpcmVkXCI6dHJ1ZSxcImFjY291bnROb25Mb2NrZWRcIjp0cnVlLFwiY3JlZGVudGlhbHNOb25FeHBpcmVkXCI6dHJ1ZSxcImVuYWJsZWRcIjp0cnVlLFwibG9naW5UeXBlXCI6XCJtZW1iZXJMb2dpblwiLFwib3BlbklkXCI6XCJvSDJIdzBET0JJY2U0NDZoVGdrV0ZxeEs5ZGljXCIsXCJwYXNzd29yZFwiOlwiJDJhJDEwJEVIeHRLa1M0eUtiSFBQUlFMRjJlM09uYmRxNjQ4ZlouRGFJRVRmQ0Q3ZG1pZ29Pb3Uxd0ZtXCIsXCJwZXJtc1wiOltdLFwic3RhdHVzXCI6MSxcInVzZXJuYW1lXCI6XCJtZW1iZXJMb2dpbm51bGxcIn0ifQ.hqzgN9TRutlRjYzbec1jBH-6d0Vt2R0j2k931KyLEoQ");
    }
}
